#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: liang kang
@contact: gangkanli1219@163.com
@desc: 
"""
from pathlib import Path
from dltools.data import BaseFileReader
from dltools.utils import log


class GoodsReader(BaseFileReader):

    def __init__(self, root, logger, use_name=False):
        if logger is not None:
            logger = logger.getChild('GoodsReader')
        else:
            logger = log.get_console_logger('GoodsReader')
        self.use_name = use_name
        super(GoodsReader, self).__init__(root, display=10, logger=logger)

    def _filter(self, file: Path):
        if file.suffix in ('.jpg', '.JPG', '.png', '.PNG'):
            if self.use_name:
                xml = file.with_name(file.name + '.xml')
            else:
                xml = file.with_name(file.stem + '.xml')
            if xml.exists():
                self._buf_data = {'image': str(file),
                                  'xml': str(xml)}
                return True
        return False
